<?php

namespace App\Controller;

use App\Logic;

class UserController extends AppBaseController
{
    private $wxUserLogic;
    private $smsLogic;
    private $userLogic;
    private $joinLogic;
    private $onlineLogic;
    public static $bindPhoneSessionName = 'bindPhoneSmsCode';

    public function _initialize()
    {
        parent::_initialize();
        $this->wxUserLogic = A('Common/WxUser', 'Logic');
        $this->smsLogic = A('Common/Sms', 'Logic');
        $this->userLogic = A('App/User', 'Logic');
        $this->joinLogic = A('App/Join', 'Logic');
        $this->onlineLogic = A('App/Online', 'Logic');
    }

    /**
     * 用户中心首页.
     */
    public function index()
    {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];
        $blanceRes = $this->userLogic->getBlanceInfo($userId);
        if (static::$res->isErr($blanceRes)) {
            $this->sysErr('账户金额数据获取失败');
        }
        $blanceInfo = $blanceRes['Result'];

        //获取收徒数量
        $recomCount = $this->userLogic->statistics(array(
            'parent_id' => $userId,
        ));

        $this->assign('userInfo', $userInfo);
        $this->assign('blanceInfo', $blanceInfo);
        $this->assign('recomCount', $recomCount);
        $this->display();
    }

    /**
     * 个人资料.
     */
    public function userInfo()
    {
        $userInfo = $this->getUserInfo();
        $this->assign('info', $userInfo);
        $this->display();
    }

    /**
     * 帮助中心.
     */
    public function help()
    {
        $list = $this->userLogic->getQuesList();

        $onlineRes = $this->onlineLogic->getInfo();
        $online = $onlineRes['Result'];

        $this->assign('list', $list);
        $this->assign('online', $online);
        $this->display();
    }

    /**
     * 获取参加明细列表.
     */
    public function taskList()
    {
        $state = I('get.state');
        $state = $state ? $state : '2';
        $this->assign('state', $state);
        $this->display();
    }

    /**
     * 绑定手机页面.
     */
    public function bindPhone()
    {
        $reback = I('get.reback');
        $reback = htmlspecialchars_decode(urldecode($reback));
        $this->assign('reback', $reback);
        $this->display();
    }

    /**
     * 发送短信验证码
     */
    public function doGetBindPhoneSmsCode()
    {
        $imgCode = I('post.imgCode');
        $imgController = A('imgCode', 'Controller');
        if (!$imgController->verityCheck($imgCode)) {
            $this->ajaxReturn(static::$res->err('图形验证码错误'));
        }

        $phone = I('post.phone');
        $phoneUserRes = $this->userLogic->findUserInfo(array(
            'phone' => $phone,
            'user_state' => array('neq', -1),
        ));
        $phoneUser = $phoneUserRes['Result'];
        if ($phoneUser) {
            $this->ajaxReturn(static::$res->err('该手机已被其它用户使用'));
        }

        $smsCode = rand(1111, 9999);
        session(static::$bindPhoneSessionName, array(
            'phone' => $phone,
            'code' => $smsCode,
        ));
        $msg = '【比特未来】您的验证码是'.$smsCode;
        $res = $this->smsLogic->singleSend($phone, $msg);
        $this->ajaxReturn($res);
    }

    /**
     * 更新用户手机号.
     */
    public function doUpdataUserPhone()
    {
        $code = I('post.code');
        if (empty($code)) {
            $this->ajaxReturn(static::$res->err('短信验证码不能为空'));
        }

        $userInfo = $this->getUserInfo();
        $id = $userInfo['id'];

        $origialCodeInfo = session(static::$bindPhoneSessionName);
        if (empty($origialCodeInfo)) {
            $this->ajaxReturn(static::$res->err('验证码已失效'));
        }

        $origialCode = $origialCodeInfo['code'];
        if ($origialCode != $code) {
            $this->ajaxReturn(static::$res->err('请输入正确的短信验证码'));
        }

        $phone = $origialCodeInfo['phone'];
        if (empty($phone)) {
            $this->ajaxReturn(static::$res->err('系统异常'));
        }

        $phoneUserRes = $this->userLogic->findUserInfo(array(
            'phone' => $phone,
            'user_state' => array('neq', -1),
        ));
        $phoneUser = $phoneUserRes['Result'];
        if ($phoneUser) {
            $this->ajaxReturn(static::$res->err('该手机已被其它用户使用'));
        }

        $bindRes = $this->wxUserLogic->updateUser($id, '', array(
            'phone' => $phone,
        ));
        $this->ajaxReturn($bindRes);
    }

    /**
     * 参与任务成员的列表.
     */
    public function doGetUserTaskList()
    {
        $state = I('post.state');
        $page = I('post.page');

        $state = $state ? $state : '1';
        $page = $page ? $page : '1';
        $size = 20;

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        if (strpos($state, ',')) {
            $state = array('in', explode(',', $state));
        }

        $listRes = $this->joinLogic->getJoinTaskList($page, $size, array(
            'user_id' => $userId,
            'state' => $state,
        ));

        $listInfo = $listRes['Result'];
        $list = $listInfo['list'];

        $count = $listInfo['count'];

        if (empty($list)) {
            $this->ajaxReturn(static::$res->err('暂无数据'));
        }

        $countPage = ceil($count / $size);
        $hasNext = $countPage > $page ? true : false;

        $this->assign('list', $list);
        $html = $this->fetch('doGetUserTaskList');
        $res = static::$res->suc('成功', array(
            'html' => $html,
            'list' => $list,
            'count' => $count,
            'hasNext' => $hasNext,
        ));
        $this->ajaxReturn($res);
    }

    public function online()
    {
        $onlineRes = $this->onlineLogic->getInfo();
        $online = $onlineRes['Result'];
        $this->assign('online', $online);
        $this->display();
    }

    /**
     * 补全用户信息.
     *
     * @return void
     */
    public function editSurveyUser()
    {
        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $userLogic = new Logic\UserLogic();
        $userParam = array(
            'id' => $userId,
        );
        $infoRes = $userLogic->findUserInfo($userParam);
        $info = $infoRes['Result'];
        $this->assign('info', $info);
        $this->display();
    }

    public function doEditUser()
    {
        $post = I('post.');
        $data = array();
        if (!empty($post['birthday'])) {
            $data['birthday'] = date('Y-m-d', strtotime($post['birthday']));
        }

        if (!empty($post['area'])) {
            $area = explode(' ', $post['area']);
            if (empty($area[0]) || empty($area[1])) {
                $this->ajaxReturn(static::$res->err('省份城市不能为空'));
            }

            $data['prov'] = $area[0];
            $data['city'] = $area[1];
        }

        $userInfo = $this->getUserInfo();
        $userId = $userInfo['id'];

        $res = $this->wxUserLogic->updateUser($userId, '', $data);
        $this->ajaxReturn($res);
    }
}
